package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.CoinPresentVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/11/17 11:23
 * 修改备注：
 * ================================================
 */
public class CoinPresentSubmitActivity extends BaseActivity {


    @Bind(R.id.sub_et_name)
    EditTextWithClear subEtName;
    @Bind(R.id.sub_et_phone)
    EditTextWithClear subEtPhone;
    @Bind(R.id.sub_iv_minus)
    ImageView subIvMinus;
    @Bind(R.id.sub_tv_number)
    TextView subTvNumber;
    @Bind(R.id.sub_iv_plus)
    ImageView subIvPlus;
    @Bind(R.id.sub_et_address)
    EditTextWithClear subEtAddress;
    @Bind(R.id.sub_tv_price)
    TextView subTvPrice;
    @Bind(R.id.sub_tv_submit)
    TextView subTvSubmit;
    @Bind(R.id.layout_address)
    LinearLayout layoutAddress;

    private CoinPresentVO coinPresentVO;
    private int selectNum = 1;

    private String buyLimitSign = ""; //是否有限购
    private String perBuyLimitNum = "";//每位用户限购数量
    private String activityPrice = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_coin_present_submit);
        setTitle("确认信息");
    }

    @Override
    public void initMember() {

        coinPresentVO = (CoinPresentVO) getIntent().getSerializableExtra("coinPresentVO");

        activityPrice = coinPresentVO.getActivityPrice();

        buyLimitSign = coinPresentVO.getBuyLimitSign();
        if (!StringUtil.isStrEmpty(buyLimitSign) && buyLimitSign.equals("1")) // 有限购
            perBuyLimitNum = coinPresentVO.getPerBuyLimitNum();

        // 判断是否为虚拟商品   0：虚拟物品；1：实体物品
        if (coinPresentVO.getType() == 0) {
            layoutAddress.setVisibility(View.GONE);
            subEtPhone.setHint("请填写充值号码");
        }

        subEtPhone.setText(UserXML.getMobilePhone(this) + "");
        subTvNumber.setText(selectNum + "");

        setPriceText(1);


    }

    private void setPriceText(int num) {
        if (StringUtil.isStrEmpty(activityPrice)) {
            subTvPrice.setText(coinPresentVO.getPrice() * num + "");
        } else {
            subTvPrice.setText(Integer.parseInt(coinPresentVO.getActivityPrice()) * selectNum + "");
        }
    }


    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_SAVE_EXCHANGE_SUCCESS:
                        new MaterialDialog.Builder(CoinPresentSubmitActivity.this)
                                .title("提示")
                                .content("兑换成功！")
                                .positiveText("确定")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        ActivityTaskManager.getInstance().closeActivity(CoinPresentDetailActivity.class);
                                        finish();
                                    }
                                })
                                .show();
                        break;
                    case MSG.MSG_SAVE_EXCHANGE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }

            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.sub_iv_minus, R.id.sub_iv_plus, R.id.sub_tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sub_iv_minus:
                if (selectNum == 1)
                    return;
                selectNum--;
                subTvNumber.setText(selectNum + "");
                setPriceText(selectNum);
                break;
            case R.id.sub_iv_plus:
                if (!StringUtil.isStrEmpty(buyLimitSign) && buyLimitSign.equals("1")) {
                    if (Integer.parseInt(subTvNumber.getText().toString()) == Integer.parseInt(perBuyLimitNum)) {
                        showSnackbar("每人限制兑换" + perBuyLimitNum + "个");
                    } else {
                        selectNum++;
                        subTvNumber.setText(selectNum + "");
                        setPriceText(selectNum);
                    }
                } else {
                    selectNum++;
                    subTvNumber.setText(selectNum + "");
                    setPriceText(selectNum);
                }

                break;
            case R.id.sub_tv_submit:

                if (StringUtil.isEditEmpty(subEtName)) {
                    showToast("请填写收货人");
                    return;
                }
                if (StringUtil.isEditEmpty(subEtPhone)) {
                    showToast("请填写手机号码");
                    return;
                }
                if (coinPresentVO.getType() == 1 && StringUtil.isEditEmpty(subEtAddress)) {
                    showToast("请填写收货地址");
                    return;
                }

                BusinessAccount.saveExchange(CoinPresentSubmitActivity.this, coinPresentVO.getId(), subEtName.getText().toString(), subEtPhone.getText().toString(), subTvNumber.getText().toString(), subEtAddress.getText().toString(), mHandler);
                break;
        }
    }
}
